home *** CD-ROM | disk | FTP | other *** search
- Path: colossus.holonet.net!russell
- From: russell@news.mdli.com (Russell Blackadar)
- Newsgroups: comp.lang.c++
- Subject: Re: Q:order of evaluation
- Date: 16 Jan 1996 17:50:58 GMT
- Organization: HoloNet National Internet Access System: 510-704-1058/modem
- Message-ID: <4dgoi2$ij8@colossus.holonet.net>
- References: <4dfhlu$a33$1@mhafn.production.compuserve.com>
- NNTP-Posting-Host: jubal.mdli.com
- X-Newsreader: TIN [version 1.2 PL2]
-
- Holger Maier (100336.3326@CompuServe.COM) wrote:
- : Consider
- : #include <iostream>
- : int main() {
- : int i=1;int j=i+(i+=1);
- ^^^^^^^^
- This expression uses i and also assigns to it, without any
- intervening sequence point. Its value is therefore undefined.
-
- : cout<<i<<','<<j<<'\n';
- : return 0;
- : }
- : on my compiler this produces 2,4
- : Looked up the ARM:
- : 5: .. The order of evaluation of subexpressions is determined by the
- : precedence and grouping of operators.
- ...
- Order of evaluation does not mean order in time; instead, it's
- a logical ordering. In your statement, the compiler is free
- to evaluate i first, then i+=1, and then the sum; or it can
- just as easily reverse the first two operations in time.
-
- Your compiler is conforming to the standard -- i.e. undefined
- behavior.
-
- : BTW: I know we should not code like that...
-
- Yes, in fact we have to be diligent it doesn't happen by accident
- in some more complicated expression.
- --
- Russell Blackadar, russell@mdli.com
-